﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.Devices.DeviceEntity.Lamp
{
    public partial class Lamp : DeviceBase
    {
        string paramName;
        string typeId;
        string statusDescription;

        public string ParamName
        {
            get
            {
                return paramName;
            }

            set
            {
                paramName = value;
            }
        }

        public override string TypeId
        {
            get
            {
                return typeId;
            }

            set
            {
                typeId = value;
                ReadParam();
            }
        }

        public override string StatusDescription
        {
            get
            {
                return statusDescription;
            }

            set
            {
                statusDescription = value;
                ChangeIcon();
            }
        }

        private void ChangeIcon()
        {
            if (!string.IsNullOrEmpty(TypeId))
            {
                switch(TypeId)
                {
                    case "Light_thb2up":
                    case "Light_jab2up":
                    case "Light_thb1up":
                    case "Light_jab1up":
                        {
                            if(StatusDescription=="点亮")
                            {
                                this.Image = ResPic.LED开下;
                            }
                            if (StatusDescription == "熄灭")
                            {
                                this.Image = ResPic.LED关下;
                            }
                        }break;
                    case "Light_thb1down":
                    case "Light_jab1down":
                    case "Light_thb2down":
                    case "Light_jab2down":
                        {
                            if (StatusDescription == "点亮")
                            {
                                this.Image = ResPic.LED开上;
                            }
                            if (StatusDescription == "熄灭")
                            {
                                this.Image = ResPic.LED关上;
                            }
                        }
                        break;
                    case "Light_ths1up":
                    case "Light_jas1up":
                    case "Light_ths2up":
                    case "Light_jas2up":
                        {
                            if (StatusDescription == "点亮")
                            {
                                this.Image = ResPic.高压钠灯开下;
                            }
                            if (StatusDescription == "熄灭")
                            {
                                this.Image = ResPic.高压钠灯关下;
                            }
                        }
                        break;
                    case "Light_ths1down":
                    case "Light_jas1down":
                    case "Light_ths2down":
                    case "Light_jas2down":
                        {
                            if (StatusDescription == "点亮")
                            {
                                this.Image = ResPic.高压钠灯开上;
                            }
                            if (StatusDescription == "熄灭")
                            {
                                this.Image = ResPic.高压钠灯关上;
                            }
                        }
                        break;
                    case "Light_thLight_Out_90W":
                    case "Light_jaLight_Out_90W":
                        {
                            this.Size = new Size(90, 90);
                            if (StatusDescription == "点亮")
                            {
                                this.Image = ResPic.引道照明开;
                            }
                            if (StatusDescription == "熄灭")
                            {
                                this.Image = ResPic.引道照明关;
                            }
                        }
                        break;
                }
            }
        }

        public Lamp()
        {
            InitializeComponent();
            this.Size = new Size(32, 25);
            this.DoubleClick += Lamp_DoubleClick;
        }

        private void Lamp_DoubleClick(object sender, EventArgs e)
        {
            LameForm f = new LameForm(this);
            f.ShowDialog();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        public override void SendCommand(CommandType commandType)
        {
            if(commandType==CommandType.照明开)
            {
                SendOn();
            }
            if(commandType==CommandType.照明关)
            {
                SendOff();
            }
        }

        private void SendOn()
        {
            DataDeal.SendCommand(DeviceId, ParamName, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "照明开启");
        }
        private void SendOff()
        {
            DataDeal.SendCommand(DeviceId, ParamName, "0");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "照明关闭");
        }

        private async void ReadParam()
        {
            DataTable dtParamIDTable = await DataDeal.ReadParamAsync(TypeId);
            foreach (DataRow item in dtParamIDTable.Rows)
            {
                switch (item["ParamStatus"].ToString())
                {
                    case "ON": ParamName = item["ParamName"].ToString(); break;                    
                }
            }
        }
    }
}
